package com.powerunion.datacollection.quartz.job;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;

import com.powerunion.datacollection.common.Constants;
import com.powerunion.datacollection.common.SystemException;
import com.powerunion.datacollection.quartz.AppTask;
import com.powerunion.datacollection.quartz.TaskScheduler;
import com.powerunion.datacollection.service.ApproveWorkItemManager;
import com.powerunion.datacollection.service.SmsManager;
import com.powerunion.datacollection.service.SubmissionWorkItemManager;
import com.powerunion.util.log.LogHelper;
/**
 * 催办任务
 * @author qyw
 *
 */
public class UrgencyTask extends AppTask {
	private static Logger logger = LogHelper.getInstance().getLogger(UrgencyTask.class);
	public void doTask() {

		JobExecutionContext jobConetext = this.getJobConetext();
		String userCode = (String)jobConetext.getJobDetail().getJobDataMap().get("userCode");
		
		String id = (String)jobConetext.getJobDetail().getJobDataMap().get("id");
		String typeStr = (String)jobConetext.getJobDetail().getJobDataMap().get("type");
		int type = Integer.parseInt(typeStr);
		String taskName = (String)jobConetext.getJobDetail().getJobDataMap().get("taskName");
		if(StringUtils.isEmpty(taskName)){
			taskName = "数据报送任务：";
		}
		String urgenContent = taskName+":"+(String)jobConetext.getJobDetail().getJobDataMap().get("urgenContent");
		logger.debug("UrgencyTask 开始保存"+taskName+"采集任务催办任务信息.");
		logger.debug("催办任务ID:"+id+"||催办人："+userCode);
		try{
			boolean isCommit = true;
			if(type == Constants.TASK_TYPE_SUBMISSIONWORK){

				SubmissionWorkItemManager sim = new SubmissionWorkItemManager();
				isCommit = sim.iSubWorkCommit(id);
				if(!isCommit){
					SmsManager sm = new SmsManager();
					sm.saveSMS(urgenContent, userCode);
				}
			}else if(type == Constants.TASK_TYPE_APPROVEWORK){
				ApproveWorkItemManager aim = new ApproveWorkItemManager();
				isCommit = aim.iApproveWorkCommit(id); 
				if(!isCommit){
					SmsManager sm = new SmsManager();
					sm.saveSMS(urgenContent, userCode);
				}
			}
			
			if(isCommit){//已经提交则停止催办
				//删除任务！
				TaskScheduler.getInstance().deleteTask(taskName);
			}
			
		}catch(Exception e){
			logger.error("", e);
			throw new SystemException("UrgencyTask throw error！");
		}
		
	}

}
